<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('money_book_users', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('book_id')->comment('账本ID');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->string('role', 20)->default('member')->comment('用户角色：admin-管理员，member-普通成员');
            $table->timestamps();

            $table->unique(['book_id', 'user_id']);
            $table->foreign('book_id')->references('id')->on('money_books')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('money_book_users');
    }
};
